<html>
<head><meta charset="utf-8"><title>rustc fails to resolve standard library items · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html">rustc fails to resolve standard library items</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219392219"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392219" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392219">(Dec 09 2020 at 20:21)</a>:</h4>
<p>It looks like r-a is failing to resolve libcore/std items on the rustc code base. I'm getting this output:</p>
<div class="codehilite"><pre><span></span><code>[ERROR project_model::workspace] cyclic deps: cargo_test_support(CrateId(156)) -&gt; cargo(CrateId(144))
[ERROR project_model::workspace] cyclic deps: rustc_std_workspace_core(CrateId(1133)) -&gt; core(CrateId(222))
[ERROR project_model::workspace] cyclic deps: wasi(CrateId(1649)) -&gt; rustc_std_workspace_alloc(CrateId(1132))
</code></pre></div>



<a name="219392270"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392270" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392270">(Dec 09 2020 at 20:21)</a>:</h4>
<p>These cyclic deps errors have shown up quite a bit in the past – is our project model broken somehow, or why do they appear?</p>



<a name="219392279"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392279" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392279">(Dec 09 2020 at 20:21)</a>:</h4>
<p>sounds like something to do with bootstrapping?</p>



<a name="219392360"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392360" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392360">(Dec 09 2020 at 20:22)</a>:</h4>
<p>although I guess libstd doesn't depend on rustc in the same sense as rust-analyzer thinks of it</p>



<a name="219392474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392474">(Dec 09 2020 at 20:23)</a>:</h4>
<p>Seems like r-a usually ends up using the standard library of whichever toolchain is installed, not libstd from the Rust checkout</p>



<a name="219392611"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392611" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392611">(Dec 09 2020 at 20:24)</a>:</h4>
<p>Looks like <span class="user-mention" data-user-id="133169">@matklad</span> has recently refactored the code dealing with cyclic deps</p>



<a name="219392651"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219392651" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219392651">(Dec 09 2020 at 20:24)</a>:</h4>
<p>ah yeah, rustc depending on master libstd strikes again <span aria-label="upside down" class="emoji emoji-1f643" role="img" title="upside down">:upside_down:</span></p>



<a name="219469379"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219469379" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219469379">(Dec 10 2020 at 13:13)</a>:</h4>
<p><span class="user-mention" data-user-id="211727">@Jonas Schievink</span> yeah, cyclic deps are a long-standing bug.</p>
<p>The problem is that cargo doesn't <em>quite</em> forbid cyclic dependencies between packages. I think what cargo allows is for <code>src/lib.rs</code> with <code>--test</code> to depend on <code>src/lib.rs</code> without <code>--test</code>, and that doesn't quite pass our logic of optimistically enabling test.</p>



<a name="219469417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219469417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219469417">(Dec 10 2020 at 13:13)</a>:</h4>
<p>That is,  to solve this properly we need to dupe packages. Let me <code>.entry().or_insert()</code> this issue....</p>



<a name="219469636"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219469636" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219469636">(Dec 10 2020 at 13:15)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/issues/2414#issuecomment-561213070">https://github.com/rust-analyzer/rust-analyzer/issues/2414#issuecomment-561213070</a></p>



<a name="219469907"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219469907" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219469907">(Dec 10 2020 at 13:18)</a>:</h4>
<p>Although I am not sure why my changes cause it to fail if it worked before...</p>



<a name="219470142"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219470142" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219470142">(Dec 10 2020 at 13:20)</a>:</h4>
<p>I see, that makes sense. Yeah, I'm not sure why it suddenly broke.</p>



<a name="219664305"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219664305" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219664305">(Dec 11 2020 at 21:25)</a>:</h4>
<p>My current workaround:</p>
<div class="codehilite" data-code-language="Diff"><pre><span></span><code><span class="gh">diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs</span>
<span class="gh">index 7f4a7e56b..47aaad197 100644</span>
<span class="gd">--- a/crates/project_model/src/workspace.rs</span>
<span class="gi">+++ b/crates/project_model/src/workspace.rs</span>
<span class="gu">@@ -358,8 +358,8 @@ fn cargo_to_crate_graph(</span>

     // Now add a dep edge from all targets of upstream to the lib
     // target of downstream.
<span class="gd">-    for pkg in cargo.packages() {</span>
<span class="gd">-        for dep in cargo[pkg].dependencies.iter() {</span>
<span class="gi">+    for pkg in cargo.packages().collect::&lt;Vec&lt;_&gt;&gt;().into_iter().rev() {</span>
<span class="gi">+        for dep in cargo[pkg].dependencies.iter().rev() {</span>
             let name = CrateName::new(&amp;dep.name).unwrap();
             if let Some(&amp;to) = pkg_to_lib_crate.get(&amp;dep.pkg) {
                 for &amp;from in pkg_crates.get(&amp;pkg).into_iter().flatten() {
</code></pre></div>



<a name="219664942"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219664942" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219664942">(Dec 11 2020 at 21:31)</a>:</h4>
<p>Welp, this line breaks r-a in libcore of a Rust checkout <a href="https://github.com/rust-lang/rust/blob/2225ee1b62ff089917434aefd9b2bf509cfa087f/library/core/src/lib.rs#L51">https://github.com/rust-lang/rust/blob/2225ee1b62ff089917434aefd9b2bf509cfa087f/library/core/src/lib.rs#L51</a></p>



<a name="219664965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219664965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219664965">(Dec 11 2020 at 21:31)</a>:</h4>
<p>At least there I know why it happens and who did it (me)</p>



<a name="219665252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/rustc%20fails%20to%20resolve%20standard%20library%20items/near/219665252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/rustc.20fails.20to.20resolve.20standard.20library.20items.html#219665252">(Dec 11 2020 at 21:33)</a>:</h4>
<p>I guess we don't set the <code>--cfg test</code> for sysroot crates, otherwise I would have broken <em>everything</em></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>